import { defineStore } from 'pinia'
import axios from 'axios'

export const useAdminStore = defineStore('admin', {
  state: () => ({
    users: [],
    adminLogs: [],
    loginLogs: [],
    loading: false,
    error: null,
    totalPages: 1,
    currentPage: 1
  }),
  
  actions: {
    async fetchUsers(page = 1, size = 10, query = '') {
      this.loading = true
      try {
        const response = await axios.get('/api/admin/users', {
          params: { page, size, query }
        })
        this.users = response.data.content
        this.totalPages = response.data.totalPages
        this.currentPage = page
        return response.data
      } catch (error) {
        this.error = error.response?.data?.message || '获取用户列表失败'
        throw error
      } finally {
        this.loading = false
      }
    },
    
    async fetchAdminLogs(page = 1, size = 10) {
      this.loading = true
      try {
        const response = await axios.get('/api/admin/logs', {
          params: { page, size }
        })
        this.adminLogs = response.data.content
        this.totalPages = response.data.totalPages
        this.currentPage = page
        return response.data
      } catch (error) {
        this.error = error.response?.data?.message || '获取管理员日志失败'
        throw error
      } finally {
        this.loading = false
      }
    },
    
    async fetchLoginLogs(page = 1, size = 10) {
      this.loading = true
      try {
        const response = await axios.get('/api/admin/login-logs', {
          params: { page, size }
        })
        this.loginLogs = response.data.content
        this.totalPages = response.data.totalPages
        this.currentPage = page
        return response.data
      } catch (error) {
        this.error = error.response?.data?.message || '获取登录日志失败'
        throw error
      } finally {
        this.loading = false
      }
    },
    
    async updateUserStatus(userId, status) {
      this.loading = true
      try {
        const response = await axios.put(`/api/admin/user/${userId}/status`, { status })
        
        // 更新本地用户状态
        const userIndex = this.users.findIndex(user => user.id === userId)
        if (userIndex !== -1) {
          this.users[userIndex].status = status
        }
        
        return response.data
      } catch (error) {
        this.error = error.response?.data?.message || '更新用户状态失败'
        throw error
      } finally {
        this.loading = false
      }
    },
    
    async createBlogType(typeData) {
      this.loading = true
      try {
        const response = await axios.post('/api/admin/blog-type/create', typeData)
        return response.data
      } catch (error) {
        this.error = error.response?.data?.message || '创建博客分类失败'
        throw error
      } finally {
        this.loading = false
      }
    },
    
    async updateBlogType(typeId, typeData) {
      this.loading = true
      try {
        const response = await axios.put(`/api/admin/blog-type/update/${typeId}`, typeData)
        return response.data
      } catch (error) {
        this.error = error.response?.data?.message || '更新博客分类失败'
        throw error
      } finally {
        this.loading = false
      }
    },
    
    async deleteBlogType(typeId) {
      this.loading = true
      try {
        const response = await axios.delete(`/api/admin/blog-type/delete/${typeId}`)
        return response.data
      } catch (error) {
        this.error = error.response?.data?.message || '删除博客分类失败'
        throw error
      } finally {
        this.loading = false
      }
    }
  }
}) 